//Copyright 2011 Google Inc. All Rights Reserved.

/**
 * @fileoverview Defines a set of behaviors that can be applied to the Layer UX
 * consoles through inheritance.  These behaviors include:
 *     disabling, visibility.
 *
 * @author jasonstredwick@google.com (Jason Stredwick)
 */


goog.provide('bite.layer.ConsoleBehavior');

goog.require('goog.string');
goog.require('goog.style');



/**
 * Binds together a set of behaviors common to all consoles.
 * @constructor
 * @export
 */
bite.layer.ConsoleBehavior = function() {
  /**
   * The element under control.
   * @type {Element}
   * @private
   */
  this.consoleBehaviorTarget_ = null;
};


/**
 * Console element property names.
 * @enum {string}
 * @private
 */
bite.layer.ConsoleBehavior.Property_ = {
  DISABLE: 'disabled',
  VISIBILITY: goog.string.toCamelCase('display')
};


/**
 * Defines the possible state values for each property.
 * @enum {string|undefined}
 * @private
 */
bite.layer.ConsoleBehavior.State_ = {
  DISABLE_OFF: undefined,
  DISABLE_ON: 'disabled',
  VISIBILITY_OFF: 'none',
  VISIBILITY_ON: 'block'
};


/**
 * Change the controlled element's disabled flag to 'disabled' marking it
 * disabled.
 * @export
 */
bite.layer.ConsoleBehavior.prototype.disable = function() {
  if (this.consoleBehaviorTarget_) {
    this.consoleBehaviorTarget_[bite.layer.ConsoleBehavior.Property_.DISABLE] =
        bite.layer.ConsoleBehavior.State_.DISABLE_ON;
  }
};


/**
 * Change the controlled element's disabled flag to undefined marking it as
 * enabled.
 * @export
 */
bite.layer.ConsoleBehavior.prototype.enable = function() {
  if (this.consoleBehaviorTarget_) {
    this.consoleBehaviorTarget_[bite.layer.ConsoleBehavior.Property_.DISABLE] =
        bite.layer.ConsoleBehavior.State_.DISABLE_OFF;
  }
};


/**
 * Change the controlled element's display to none.
 * @export
 */
bite.layer.ConsoleBehavior.prototype.hide = function() {
  if (this.consoleBehaviorTarget_) {
    goog.style.setStyle(this.consoleBehaviorTarget_,
                        bite.layer.ConsoleBehavior.Property_.VISIBILITY,
                        bite.layer.ConsoleBehavior.State_.VISIBILITY_OFF);
  }
};


/**
 * Tells whether or not the controlled element is disabled.
 * @return {boolean} True if disabled.
 * @export
 */
bite.layer.ConsoleBehavior.prototype.isDisabled = function() {
  var key = bite.layer.ConsoleBehavior.Property_.DISABLE;

  if (this.consoleBehaviorTarget_ &&
      this.consoleBehaviorTarget_[key] ==
        bite.layer.ConsoleBehavior.State_.DISABLE_ON) {
    return true;
  }

  return false;
};


/**
 * Tells whether or not the element is visible; i.e. display is set to block.
 * @return {boolean} True if visible.
 * @export
 */
bite.layer.ConsoleBehavior.prototype.isVisible = function() {
  if (!this.consoleBehaviorTarget_) {
    return false;
  }

  var state = goog.style.getStyle(this.consoleBehaviorTarget_,
      bite.layer.ConsoleBehavior.Property_.VISIBILITY);
  return state == bite.layer.ConsoleBehavior.State_.VISIBILITY_ON;
};


/**
 * Sets the contolled element to the given value or null to clear.
 * @param {Element} element The element to control.
 * @export
 */
bite.layer.ConsoleBehavior.prototype.setBehavior = function(element) {
  this.consoleBehaviorTarget_ = element;
};


/**
 * Change the controlled element's display to block.
 * @export
 */
bite.layer.ConsoleBehavior.prototype.show = function() {
  if (this.consoleBehaviorTarget_) {
    goog.style.setStyle(this.consoleBehaviorTarget_,
                        bite.layer.ConsoleBehavior.Property_.VISIBILITY,
                        bite.layer.ConsoleBehavior.State_.VISIBILITY_ON);
  }
};

